import axios from 'axios'
import store from '@/store'
// import router from '@/router/index'
// import { getToken } from '@/utils/auth'

// create an axios instance
const service = axios.create({
    baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
    // withCredentials: true, // send cookies when cross-domain requests
    timeout: 5000 // request timeout
})

service.interceptors.request.use(
    config => {
        if (store.state.user.token) {
            config['headers']['Authorization'] = `Bearer ${store.state.user.token}`
        }
        return config
    }, error => {
        return Promise.reject(error)
    }
)

// response interceptor
service.interceptors.response.use(
    response => {
        return response.data
    },
    error => {
        return Promise.reject(error) // 返回执行错误 让当前的执行链跳出成功 直接进入 catch
    }
)

export default service